// api.js
// 统一的请求地址前缀
const baseURL = 'http://localhost:8082';
	var api = '/api'
	var session=''
	//判断是否是浏览器启动，如果是，方便开发手动加session
	if (process.env.VUE_APP_PLATFORM == 'h5') {
		api = '/api'
		// session='f85a0bd35aab4cb7abfd5ebeb994c11b'
	uni.setStorage({
	  key: 'sessionId',
	  data: session,
	  success: function() {
	    console.log('sessionId stored successfully');
	  },
	  fail: function(error) {
	    console.error('Failed to store sessionId:', error);
	  }
	});

	} else {
		api = 'https://app.tongfupay.com/'
		
	}
	
				const os = uni.getSystemInfoSync();
				console.log(os);
			
const data = {
	  "appVersion": "2.0.3",
	  "deviceModel": "HUAWEI--ABR-AL60",
	  "firstTime": "false",
	  "getuiPushId": "1d9f77f09f48906a7f0bf68c99e45920",
	  "guid": "2a93acc7957448e78015098dc8e734fb",
	  "h5Module": {
	    "css": "2.12",
	    "images": "2.12",
	    "lib": "2.12",
	    "js": "2.12",
	    "html": "2.12",
	    "vuepages": "2.85",
	    "base": "2.12"
	  },
	  "hotfixVersion": "",
	  "imei": "00000000-40a5-1d4f-0000-000054ce4aa6",
	  "networkType": "0",
	  "platform": "1",
	  "sysVersion": "31",
	  "type": "21"
	
}

uni.request({
    url: api+'/app/device/login.do', // 请求地址
    method: 'POST', // 请求方法
    data: data, // 请求数据
    success: (res) => { // 请求成功的回调函数
 // 打印返回的数据
		session=res.data.resultObject.sessionId
		uni.setStorage({
		  key: 'sessionId',
		  data: session,
		  success: function() {
		    console.log('sessionId stored successfully');
		  },
		  fail: function(error) {
		    console.error('Failed to store sessionId:', error);
		  }
		});

    },
    fail: (err) => { // 请求失败的回调函数
        console.log(err); 
		uni.showModal({
		  title: '提示',
		  content:` ${JSON.stringify(err)}`,
		  showCancel: false, // 是否显示取消按钮
		  confirmText: '错误啦22222222', // 确认按钮的文字
		  success(res) {
		    if (res.confirm) {
		      console.log('用户点击了确定按钮');
		    }
		  }
		});// 打印错误信息
    }
});
// 封装 uni.request
const request = (url, method, data = {}) => {


	return new Promise((resolve, reject) => {
		//判断试用什么启动项目，如果是h5就走代理
		if (process.env.VUE_APP_PLATFORM == 'h5') {
			api = '/api'
			console.log(1);
		} else {
			api = 'https://app.tongfupay.com/'
		}

		uni.request({
			url: api + url,
			method,
			data,
			header: {
				'Content-Type': 'application/json', // 设置请求头为 JSON 格式
				'sessionId': session, // 添加 sessionId，根据你的需要修改
				'rc': '110100'
			},
			success: (res) => {
				// 请求成功时
				
				if (res.statusCode === 200) {

					resolve(res.data);
				} else {

					reject(res);
				}
			},
			fail: (err) => {
				// 请求失败时
				console.log(url, 123);
				reject(err);
			},
		});
	});
};

// 封装 showToast 工具函数
const showToast = (title, icon = 'none', duration = 2000) => {
	uni.showToast({
		title,
		icon,
		duration,
	});
};

export {
	request,
	showToast
};